package com.xialinrui.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xialinrui.entity.LoginUser;
import com.xialinrui.entity.ResponseResult;
import com.xialinrui.entity.User;
import com.xialinrui.service.UserService;
import com.xialinrui.utils.RedisCache;
import com.xialinrui.vo.PasswordChangeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xialinrui
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Resource
    private RedisCache redisCache;

    @Resource
    private UserService userService;

    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user) {
        return userService.login(user);
    }

    @PostMapping("/logout")
    public ResponseResult logout() {
        return userService.logout();
    }

    @PutMapping("/password")
    public ResponseResult changePassword(@RequestBody PasswordChangeVo passwordChangeVo) {
        LoginUser currentUserDetails = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = currentUserDetails.getUsername();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        queryWrapper.eq("username", username);
        User user = userService.getOne(queryWrapper);
        if (!passwordEncoder.matches(passwordChangeVo.getOldPassword(), user.getPassword())) {
            return new ResponseResult(400, "原始密码错误~");
        }
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("password", passwordEncoder.encode(passwordChangeVo.getNewPassword())).eq("username", username);
        userService.update(updateWrapper);
        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder
                .getContext()
                .getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Long userid = loginUser.getUser().getUserId();
        //删除redis中的值
        redisCache.deleteObject("login:" + userid);
        //返回信息
        return new ResponseResult(200, "修改密码成功，请重新登录～");
    }

    @GetMapping("/all/time")
    public ResponseResult<Integer> allTime() {
        Integer allTime = userService.allTime();
        return new ResponseResult(200, "查询成功", allTime);
    }

    @GetMapping("/all/acquirement")
    public ResponseResult<Map<String, Object>> allAcquirement() {
        HashMap<String, Object> resMap = new HashMap<>(2);
        resMap.put("wholeMoney", userService.getAllMoney());
        resMap.put("wholeLove", userService.getAllLove());
        return new ResponseResult(200, "查询成功", resMap);
    }

    @PutMapping("/set/rule")
    public ResponseResult setRule(@RequestParam Double getMoneyPerMinute, @RequestParam Integer getLovePerMinute) {
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.set("get_money_per_minute", getMoneyPerMinute).set("get_love_per_minute", getLovePerMinute).eq("user_id", 1);
        userService.update(userUpdateWrapper);
        return new ResponseResult(200, "修改成功");
    }

}
